<!--
 * @Author: your name
 * @Date: 2021-12-27 08:45:45
 * @LastEditTime: 2021-12-27 11:27:33
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \shop\src\components\user\address\edit.vue
-->
<template>
     <!-- 导航栏 -->
     <van-nav-bar title="编辑收货地址" left-text="返回" left-arrow @click-left="back" />

     <!-- 新增地址 -->
     <van-address-edit
     :area-list="areaList"
     show-set-default
     show-delete
     :area-columns-placeholder="['请选择', '请选择', '请选择']"
     :address-info="AddressInfo"
     @save="onSave"
     @delete="onDelete"
     />
</template>
<script>
    import { areaList } from '@vant/area-data'
    export default {
        name: 'AddressEdit',
        emits: ['toggle'],
        created() {
            //触发父组件的事件
            this.$emit('toggle', false)

            this.addInfo()
        },
        data(){
            return { 
                areaList,
                id: this.$route.query.id ? this.$route.query.id : 0,
                LoginUser: this.$cookies.get('LoginUser'),
                AddressInfo: {}
            }
        },
        methods: {
            back() {
                this.$router.go(-1)
            },
            // 编辑保存
            async onSave(content) {
              
                //组装数据
                var data = {
                    id: content.id,
                    consignee: content.name,
                    mobile: content.tel,
                    code: content.areaCode,
                    address: content.addressDetail,
                    status: content.isDefault ? 1 : 0,
                    userid: this.LoginUser.id
                }

                var result = await this.$api.AddressUpdate(data)

                
                if(result.code == 1){
                     //提醒信息 提醒完之后返回上一层
                    this.$notify({
                        type:'success',
                        message:result.msg,
                        duration:1000,
                        onClose: () => {
                        this.$router.go(-1)
                        }
                    })
                }else{
                    //添加失败
                    this.$notify({
                        message:result.msg,
                        duration:1000,
                    })
                }

            },
            // 删除
            onDelete(content) {
                //弹出确认对话框
                this.$dialog.confirm({
                'title':'收货地址',
                'message': '是否确认删除收货地址'
                })
                .then(async () => {
                    var data = {
                        id: content.id,
                        userid: this.LoginUser.id,
                        status: content.status
                    }
                    var result = await this.$api.AddressDelete(data)

                    if(result.code == 1){
                     //提醒信息 提醒完之后返回上一层
                    this.$notify({
                        type:'success',
                        message:result.msg,
                        duration:1000,
                        onClose: () => {
                        this.$router.go(-1)
                        }
                        })
                    }else{
                        //删除失败
                        this.$notify({
                            message:result.msg,
                            duration:1000,
                        })
                    }
                    
                })
                .catch(() => {})
            },
            // 查询详情
            async addInfo() {
                var result = await this.$api.AddressInfos({id: this.id, userid: this.LoginUser.id})
                if(result.code == 1){
                    
                    var data = result.data

                    //重新组装我们的数据
                    var address = {
                        id: data.id,
                        name: data.consignee,
                        tel: data.mobile,
                        addressDetail: data.address,
                        isDefault: data.status==1 ?  true : false
                    }
                    if(data.province){
                        address.province = data.province.name
                        address.areaCode = data.province.code
                    }

                    if(data.citys){
                        address.citys = data.citys.name
                        address.areaCode = data.citys.code
                    }

                    
                    if(data.districts){
                        address.districts = data.districts.name
                        address.areaCode = data.districts.code
                    }

                    this.AddressInfo = address
                }else{
                    //查询
                    this.$notify({
                        message:result.msg,
                        duration:1000,
                    })
                }
            }
        }
    }
</script>